using UnityEngine;
using System.Collections;

/*
Respawn: Allows players to respawn to this point in the level, effectively saving their progress.
The Respawn object has three main states and one interim state: Inactive, Active and Respawn, plus Triggered.
- Inactive: Player hasn't reached this point and the player will not respawn here.
- Active: Player has touched this respawn point, so the player will respawn here.
- Respawn: Player is respawning at this respawn point.
Each state has its own visual effect(s).
Respawn objects also require a simple collider, so the player can activate them. The collider is set as a trigger.
*/

public class Respawn : MonoBehaviour
{
	//To be instantiated as initial respawn
	public Respawn initialRespawn;
	public int RespawnState = 0;
	
	//Sound Effects
	public AudioClip SFXPlayerRespawn;
	public AudioClip SFXRespawnActivate;
	public AudioClip SFXRespawnActiveLoop;
	
	//Volume for single play sounds
	public float SFXVolume;
	
	//References for respawn particle emitters
	private ParticleEmitter emitterActive;
	private ParticleEmitter emitterInactive;
	private ParticleEmitter emitterRespawn1;
	private ParticleEmitter emitterRespawn2;
	private ParticleEmitter emitterRespawn3;
	
	//Respawn Light
	private Light respawnLight;
	
	//Active Respawn Point
	//Static, so all Respawn prefabs will share this
	public static Respawn currentRespawn;
	
	
	void Start()
	{
		//Initialize Objects
		emitterActive = transform.Find("RSParticlesActive").GetComponent<ParticleEmitter>();
		emitterInactive = transform.Find("RSParticlesInactive").GetComponent<ParticleEmitter>();
		emitterRespawn1 = transform.Find("RSParticlesRespawn1").GetComponent<ParticleEmitter>();
		emitterRespawn2 = transform.Find("RSParticlesRespawn2").GetComponent<ParticleEmitter>();
		emitterRespawn3 = transform.Find("RSParticlesRespawn3").GetComponent<ParticleEmitter>();
		
		respawnLight = transform.Find("RSSpotlight").GetComponent<Light>();
		
		RespawnState = 0;
		
		//Setup the loop "RespawnActive" sound, leave it off
		if(SFXRespawnActiveLoop)
		{
			audio.clip = SFXRespawnActiveLoop;
			audio.loop = true;
			audio.playOnAwake = false;
		}
		
		//Assign respawn point
		currentRespawn = initialRespawn;
		if(currentRespawn == this)
		{
			SetActive();
		}
		
	}
	
	void OnTriggerEnter()
	{
		if(currentRespawn != this)
		{
			//Turn off old respawn
			currentRespawn.SetInactive();
			
			//Play "Activated" sound effect
			if(SFXRespawnActivate)
			{
				AudioSource.PlayClipAtPoint(SFXRespawnActivate, transform.position, SFXVolume);
			}
			
			//Set this as current respawn and make it visible
			currentRespawn = this;
			
			SetActive();
		}
	}
	
	void SetActive()
	{
		emitterActive.emit = true;
		emitterInactive.emit = false;
		respawnLight.intensity = 1.5f;
		
		//Play sound assigned in inspector
		audio.Play();
	}
	
	void SetInactive()
	{
		emitterActive.emit = false;
		emitterInactive.emit = true;
		respawnLight.intensity = 1.5f;
		
		//Stop playing active sound
		audio.Stop();
	}
	
	public IEnumerator FireEffect()
	{
		emitterRespawn1.Emit();
		emitterRespawn2.Emit();
		emitterRespawn3.Emit();
		
		respawnLight.intensity = 3.5f;
		
		if(SFXPlayerRespawn)
		{
			//If a "player is respawning" sound effect exists, play 
			AudioSource.PlayClipAtPoint(SFXPlayerRespawn, transform.position, SFXVolume);
		}
		
		//Dim the light after a brief Flare
		yield return new WaitForSeconds(2);
		
		respawnLight.intensity = 2.0f;
	}
	
	

}